package com.hhsj.dynamicui.model.base;


import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class BaseModel<T extends JSONAble> implements Serializable, JSONAble {

    public static BaseModel newEmpty(String code, String msg) {
        return new BaseModel<>(code, msg, Empty.DEFAULT);
    }

    public BaseModel() {
    }

    public BaseModel(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public BaseModel(String code, String msg, List<T> dataList) {
        this.code = code;
        this.msg = msg;
        this.dataList = dataList;
    }

    public String code;
    public String msg;
    public T data;
    public List<T> dataList;

    /**
     * {
     * "code":"0",
     * "msg":"成功",
     * "data":[]
     * }
     * <p>
     * {
     * "code":"0",
     * "msg":"成功",
     * "data":{}
     * }
     */


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public List<T> getDataList() {
        return dataList;
    }

    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
    }

    @Override
    public final String toJSONString() {
        if (data != null) {
            return "{" +
                    "\n    \"code\":\"" + code + "\"," +
                    "\n    \"msg\":\"" + msg + "\"," +
                    "\n    \"data\":" + data.toJSONString() +
                    "\n}";
        }

        if (dataList != null) {

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < dataList.size(); i++) {
                builder.append(dataList.get(i).toJSONString());
                if (i < dataList.size() - 1) {
                    builder.append(",");
                }
            }

            return "{" +
                    "\n    \"code\":\"" + code + "\"," +
                    "\n    \"msg\":\"" + msg + "\",\n" +
                    "\n    \"data\":[" + builder.toString() +
                    "\n]" +
                    "\n}";
        }


        return "{" +
                "\n    \"code\":\"" + code + "\"," +
                "\n    \"msg\":\"" + msg + "\"," +
                "\n    \"data\":{}" +
                "\n}";

    }

    public void fromJson(JSONObject object, Class<T> tClass) {
        code = object.optString("code", "0");
        msg = object.optString("msg", "");
        Gson gson = new Gson();
        JSONObject dataObject = object.optJSONObject("data");
        if (dataObject != null) {
            data = gson.fromJson(dataObject.toString(), tClass);
        }
        if (data == null) {
            JSONArray array = object.optJSONArray("data");
            int size = array == null ? 0 : array.length();
            if (size > 0) {
                dataList = new ArrayList<>();
                for (int i = 0; i < size; i++) {
                    dataList.add(gson.fromJson(array.optJSONObject(i).toString(), tClass));
                }
            }

        }
    }
}
